//
//  YAYIViewController.m
//  多点执业
//
//  Created by hxp on 16/6/22.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIResumeViewController.h"
#import "YAYIEditResumeController.h"

#import "YAYIEditResumeCell.h"
#import "Masonry/Masonry.h"
#import "YAYReviewController.h"


#define YAYIFont(t) [UIFont systemFontOfSize:t]

@interface YAYIResumeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UILabel *mobileLabel;
@property (nonatomic,strong)UILabel *workLabel;
@property (nonatomic,strong)UILabel *addressLabel;
@property (nonatomic,strong)UILabel *workExperise;
@property (nonatomic,strong)NSString *work;
@property (nonatomic,strong)NSString *education;
@property (nonatomic,strong)NSMutableArray *dataAry;
@property (nonatomic,strong)NSMutableArray *imageViewAry;
@property (nonatomic,strong)UILabel *line;
@property (nonatomic,strong)UILabel *titleView;
@property (nonatomic,strong)UIView  *footView;
@property (nonatomic,strong)NSMutableArray *imageAry;
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)UIButton *reviewBtn;


@property (nonatomic, assign) NSInteger oldImageRowCount;

@end


@implementation YAYIResumeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的简历";
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dataAry = [NSMutableArray array];
    self.imageAry = [NSMutableArray array];
    
    [self createRightBtn];
    [self createDataSource];
    [self dataSource];
    [self createTableView];
    [self bottomBtn];
}


-(void)createRightBtn{
    
    NSString *rightBtnTitleStr = @"修改";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:rightBtnTitleStr forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    
    button.frame = (CGRect){{0, 0}, CGSizeMake(70, 30)};
    
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}


-(UIView *)headerView{
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    bgView.frame  = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width ,220);
    
    UIView *headerView = [UIView new];
    headerView.frame = CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 210);
    headerView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:headerView];
    UILabel *dotLabel = [UILabel new];
    dotLabel.frame = CGRectMake(0, 4, 4, 30);
    dotLabel.backgroundColor = [UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    [headerView addSubview:dotLabel];
    
    UILabel *title = [UILabel new];
    title.text = @"基本信息";
    title.frame = CGRectMake(CGRectGetMaxX(dotLabel.frame) + 15, CGRectGetMinY(dotLabel.frame), 100, 30);
    title.textColor = YAYIColor(83,165, 243);
    //[UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    title.font = YAYIFont(14);
    [headerView addSubview:title];
    
    UILabel *line = [UILabel new];
    line.frame = CGRectMake(0, CGRectGetMaxY(dotLabel.frame) + 4, [UIScreen mainScreen].bounds.size.width, 0.2);
    line.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:line];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(line.frame) + 8, 200, 30);
    self.nameLabel.font = YAYIFont(14);
    self.nameLabel.textColor = YAYIColor(122, 123,125);
    [headerView addSubview:self.nameLabel];
    
    self.mobileLabel = [UILabel new];
    self.mobileLabel.font = YAYIFont(14);
    self.mobileLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.nameLabel.frame)+4, 180, 30);
    self.mobileLabel.textColor = YAYIColor(122, 123,125);
    [headerView addSubview:self.mobileLabel];
    
    self.workLabel = [UILabel new];
    self.workLabel.font = YAYIFont(14);
    self.workLabel.textColor = YAYIColor(122, 123,125);
    self.workLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.mobileLabel.frame), 180, 30);
    [headerView addSubview:self.workLabel];
    
    self.addressLabel = [UILabel new];
    self.addressLabel.textColor = YAYIColor(122, 123, 125);
    self.addressLabel.font = YAYIFont(14);
    self.addressLabel.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.workLabel.frame), 280, 30);
    [headerView addSubview:self.addressLabel];
    
    
    self.workExperise = [UILabel new];
    self.workExperise.frame = CGRectMake(CGRectGetMinX(title.frame), CGRectGetMaxY(self.addressLabel.frame), 180, 30);
    self.workExperise.font = YAYIFont(14);
    self.workExperise.textColor = YAYIColor(122, 123,125);
    [headerView addSubview:self.workExperise];
    
    return bgView;
}


-(void)dataSource{
    self.nameLabel.text =    @"姓       名：张山";
    self.mobileLabel.text =  @"电       话：17787653364";
    self.workLabel.text =    @"职       务：护士";
    self.addressLabel.text = @"工作地点：西湖区西溪路525号浙大科技园";
    self.workExperise.text = @"工作年限：请选择";
}


-(UIView *)footerView{
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    bgView.clipsToBounds = YES;
    bgView.layer.masksToBounds = YES;
    self.bgView = bgView;
    bgView.frame  = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width ,300);
//    self.tableView.tableFooterView = bgView;
    
    
    UIView *footerView = [UIView new];
    footerView.backgroundColor = [UIColor whiteColor];
    footerView.frame = CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width ,0);
    footerView.clipsToBounds = YES;
    footerView.layer.masksToBounds = YES;
    self.footView = footerView;
    [bgView addSubview:footerView];
    
    UILabel *dotLabel = [UILabel new];
    dotLabel.frame = CGRectMake(0, 4, 4, 30);
    dotLabel.backgroundColor = [UIColor colorWithRed:83/255.0 green:165/255.0 blue:243/255.0 alpha:1];
    [footerView addSubview:dotLabel];
    
    UILabel *title = [UILabel new];
    title.text = @"相关证书";
    title.frame = CGRectMake(CGRectGetMaxX(dotLabel.frame) + 20, CGRectGetMinY(dotLabel.frame), 100, 30);
    title.textColor = YAYIColor(83,165, 243);
    title.font = YAYIFont(14);
    [footerView addSubview:title];
    
    UILabel *line = [UILabel new];
    line.frame = CGRectMake(0, CGRectGetMaxY(title.frame) + 4, [UIScreen mainScreen].bounds.size.width, 0.2);
    self.line = line;
    line.backgroundColor = [UIColor lightGrayColor];
    [footerView addSubview:line];
    
    
    footerView.height = CGRectGetMaxY(line.frame) + 15;
    bgView.height = CGRectGetMaxY(line.frame) + 25;
    
    self.tableView.tableFooterView = bgView;
    self.oldImageRowCount = 0;
    
    return bgView;
}


-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_width, SCREEN_height)];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [self footerView];
    self.tableView.tableHeaderView = [self headerView];
    [self.view addSubview:self.tableView];
}


-(void)createImageView:(NSMutableArray *)arry{
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 10 * 2)/4.0;
    int i =0;
    
    for (UIView *view in self.footView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    for (NSString *url in self.imageAry) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + i%4 * width, CGRectGetMaxY(self.line.frame) + 10 +(i/4 * 90), 80, 80)];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        imageView.userInteractionEnabled = YES;
        [self.footView addSubview:imageView];
        
        i++;
    }
    CGRect rect = self.bgView.frame;
    if (self.imageAry.count == 0) {
      rect.size.height = 70;
    }else{
      rect.size.height = 48+(self.imageAry.count/4 * 90)+70+40;
    }
    
    
    NSInteger rowCount = (self.imageAry.count - 1) / 4 + 1;
    if (self.imageAry.count == 0) {
        rowCount = 0;
    }
    
    self.bgView.height += (rowCount - self.oldImageRowCount) * 90;
    self.footView.height += (rowCount - self.oldImageRowCount) * 90;

//    self.tableView.contentSize = CGSizeMake(SCREEN_width, self.tableView.contentSize.height + (rowCount - self.oldImageRowCount ) * 90);
    self.tableView.tableFooterView = self.bgView;
    
    self.oldImageRowCount = rowCount;
}


#pragma mark - 获取数据
- (void)refreshPage
{

    [self createDataSource];
}


-(void)createDataSource
{
    
    [self.dataAry removeAllObjects];
    [self.imageAry removeAllObjects];
    [YAYINetWorking getWithURL:resumeInfo_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            
            YAYILog(@"返回:%@", dict);
            
            NSDictionary *data = dict[@"data"];
            self.nameLabel.text =    [NSString stringWithFormat:@"姓       名：%@", data[@"fullname"]];
            self.mobileLabel.text =  [NSString stringWithFormat:@"电       话：%@", data[@"mobile"]];
            self.workLabel.text =    [NSString stringWithFormat:@"职       务：%@", data[@"title"]];
            
            NSString *workAreaStr = (!data[@"defaulthosname"] || [data[@"defaulthosname"] isKindOfClass:[NSNull class]]) ? @"": data[@"defaulthosname"];
            self.addressLabel.text = [NSString stringWithFormat:@"工作地点：%@", workAreaStr];
            self.workExperise.text = [NSString stringWithFormat:@"工作年限：%@",data[@"workyears"]];
            self.education = data[@"education"];
            self.work = data[@"work"];
            [self.dataAry addObject:data[@"education"]];
            [self.dataAry addObject:data[@"work"]];
            
            NSDictionary *certificateUrl = data[@"certificateUrl"];
            
            for (int i = 0; i < certificateUrl.allKeys.count; i++) {
                NSString *url = [NSString stringWithFormat:@"%@/%@",API,certificateUrl[[NSString stringWithFormat:@"certificate%d",i]]];
                [self.imageAry addObject:url];
                
            }
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:dict[@"mobile"] forKey:@"mobile"];
            [dict setValue:dict[@"v"] forKey:@"v"];
            [user setObject:dict forKey:@"reviewed"];
            [user synchronize];
            
            if ([user objectForKey:@"reviewed"] != nil) {
                NSDictionary *dict = [user objectForKey:@"reviewed"];
                NSString *mobile = dict[@"mobile"];
//                NSString *v = dict[@"v"];
                if ([mobile isEqualToString:data[@"mobile"]]) {
                    self.reviewBtn.hidden = YES;
                    
                }
            }
            [self createImageView:_imageAry];
        }
        
        [self.tableView reloadData];
    }];
}


#pragma mark - 点击事件
-(void)switchView:(UIButton *)sender{
    
    YAYILog(@"table Frame:%@", NSStringFromCGRect(self.tableView.frame));
    YAYILog(@"table Frame:%@", NSStringFromUIEdgeInsets(self.tableView.contentInset));
    
    YAYIEditResumeController *editVc = [[YAYIEditResumeController alloc] init];
    [self.navigationController pushViewController:editVc animated:YES];
    //    self.editResumeView = [YAYIResumeViewController  new];
    //    [self addChildViewController:_editResumeView];
    //    [self.view addSubview:self.editResumeView.view];
    //
    //    [sender setTitle:@"修改" forState:UIControlStateNormal];
    
}


-(void)bottomBtn{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    self.reviewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.reviewBtn setTitle:@"查看简历审核结果" forState:UIControlStateNormal];
    [self.reviewBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.reviewBtn.backgroundColor = YAYIColor(83,165, 243);
    [self.reviewBtn addTarget:self action:@selector(auditResult:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.reviewBtn];
    [self.reviewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(width, 40));
    }];
}


-(void)auditResult:(UIButton *)sender{
    
    YAYReviewController *review = [YAYReviewController new];
    review.refreshedOption = ^{
        [self createDataSource];
    };
    [self.navigationController pushViewController:review animated:YES];
}


#pragma mark - 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"cell";
    YAYIEditResumeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[YAYIEditResumeCell  alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"教育经历";
        cell.contentString = self.dataAry[indexPath.row];
        
    }else{
        cell.titleLabel.text = @"工作经历";
        cell.contentString = self.dataAry[indexPath.row];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    return cell;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataAry.count > 0) {
         CGFloat height = [self heightRowLabel:[UIFont systemFontOfSize:14] content:self.dataAry[indexPath.row]];
        return 30 + 20 + 10 +10 + height;
    }
    return 30 + 10 + 10 ;
}


-(CGFloat)heightRowLabel:(UIFont *)font content:(NSString *)content{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(width - 20, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    
    return size.height;
}

@end
